package com.banciyuan.bcywebview.biz.main.discover.topcreator;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.biz.main.discover.top.TopFragment;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

public class TopCreatorActivity extends BaseActivity implements View.OnClickListener {
    protected TabLayout mTabLayout;
    private ViewPager mViewPager;
    private String[] topCreatorNames;
    private List<TopFragment> mTopFragmentList = new ArrayList<>();
    private int tabType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.top_user_activity);
        initArgs();
        initActionbar();
        initUi();
    }

    @Override
    protected void initArgs() {
        tabType = getIntent().getIntExtra(gotoUtil.INTENT_VALUE_ONE, TopFragment.ILLUST_TAG);
        topCreatorNames = getResources().getStringArray(R.array.top_creators);
        addTopCreatorFragment(TopFragment.ILLUST_TAG);
        addTopCreatorFragment(TopFragment.WRITER_TAG);
        addTopCreatorFragment(TopFragment.COS_TAG);
    }


    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.good_creator));
    }

    @Override
    protected void initUi() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.mViewpager);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mTopFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mTopFragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return topCreatorNames[position];
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
        setViewPagerCurrentItem();
    }

    private void setViewPagerCurrentItem() {
        if (tabType == TopFragment.WRITER_TAG)
            mViewPager.setCurrentItem(1);
        else if (tabType == TopFragment.COS_TAG)
            mViewPager.setCurrentItem(2);
    }

    private void addTopCreatorFragment(int type) {
        TopFragment topFragment = new TopFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(gotoUtil.INTENT_VALUE_ONE, type);
        topFragment.setArguments(bundle);
        mTopFragmentList.add(topFragment);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_action_bar_home:
                finish();
                break;
        }
    }
}
